<?php

namespace api\modules\v1\controllers;

use Yii;
use common\helpers\CommonHelper;
use api\controllers\OnAuthController;
use api\modules\v1\forms\LoginForm;
use services\api\AccessTokenService;

/**
 * 登录接口
 *
 * Class SiteController
 * @package api\modules\v1\controllers
 */
class SiteController extends OnAuthController
{
    public $modelClass = '';

    /**
     * 不用进行登录验证的方法
     *
     * 例如： ['index', 'update', 'create', 'view', 'delete']
     * 默认全部需要验证
     *
     * @var array
     */
    protected $optional = ['down-surplus', 'refresh', 'up-pwd', 'login'];

//    public function actionDownSurplus()
//    {
//        $service = new UserSurplusService();
//        $service->statisticalDrug(3,3);
//    }

    /**
     * @api {post} /v1/site/login 用户登录
     * @apiName login
     * @apiGroup Site
     *
     * @apiDescription 登录完成调用/v1/member/permissions,获取用户权限信息
     *
     * @apiParam {String} mobile                    手机号
     * @apiParam {String} password                  密码
     *
     * @apiSuccess {String} code                    状态码
     * @apiSuccess {String} message                 提示信息
     * @apiSuccess {Object} data                    返回内容
     * @apiSuccess {String} data.access_token       token令牌
     * @apiSuccess {Object} data.info               用户信息
     * @apiSuccess {Number} data.info.member_id     用户ID
     * @apiSuccess {String} data.info.username      用户名
     * @apiSuccess {String} data.info.realname      真实姓名
     * @apiSuccess {String} data.info.email         邮箱
     * @apiSuccess {String} data.info.mobile        手机号
     * @apiSuccess {Number} data.info.is_super      是否超管【1是、0否】
     * @apiSuccess {Number} data.info.project_id    默认项目ID
     *
     */
    public function actionLogin()
    {
        $param = Yii::$app->request->post();
        $param['username'] = trim($param['mobile'] ?? '');

        $model = new LoginForm();
        $model->attributes = $param;

        if ($model->validate()) {
            $accessTokenService = new AccessTokenService();
            return $accessTokenService->getAccessToken($model->getUser());
        }

        // 返回数据验证失败
        return CommonHelper::api(422, $this->getError($model));
    }

    /**
     * 登出
     *
     * @return array|mixed
     */
    public function actionLogout()
    {
        $accessTokenService = new AccessTokenService();
        if ($accessTokenService->disableByAccessToken(Yii::$app->user->identity->access_token)) {
            return CommonHelper::api(200, '退出成功');
        }

        return CommonHelper::api(200, '退出失败');
    }

    /**
     * @api {get} /v1/site/create-voice-queue       创建单设备历史通话队列
     * @apiName create-voice-queue
     * @apiGroup Site
     *
     * @apiParam {String} wechatid                  微信id
     * @apiParam {String} imei                      imei
     * @apiParam {String} start_time                开始时间
     * @apiParam {String} end_time                  结束时间
     * @apiParam {String} type                      类型【java、swoole】
     *
     * @apiSuccess {Boolen} data                    true or false
     *
     */
    public function actionCreateVoiceQueue()
    {
        $params = Yii::$app->request->get();
        $params['wechatid'] = $params['wechatid'] ?? '';
        $params['imei'] = $params['imei'] ?? '';
        $params['start_time'] = $params['start_time'] ?? '';
        $params['end_time'] = $params['end_time'] ?? '';
        $params['type'] = $params['type'] ?? 'java';

        return CommonHelper::dengta_request('/v1/baiyou/create-voice-queue', $params);
    }

}
